package com.globant.util.email;

import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

/**
 * Simple email sender. This class sends e-mails using the
 * information obtained from the Spring-Module.xml file.
 * <p>
 * Based on the EmailSender class at com.globant.dashboard.util.email
 * 
 * @author		gonzalo.zarza
 * @version	20130124v1.1
 */

public class EmailSender{
	
	/* 
	================================================================================================
	 Fields
	================================================================================================
	*/
	private MailSender mailSender;
	private String from;
	private String to;
	private String subject;
	private String text; 
	
	/* 
	================================================================================================
	 Methods
	================================================================================================
	*/
	/**
	 * Sends a new email using the information received through the method's parameters.
	 * 
	 * @param from				sender email address
	 * @param to				destination email address
	 * @param subject			email subject
	 * @param msg				email body text
	 * @throws MailException	if the email cannot be sent
	 */
	public void sendMail(String from, String to, String subject, String msg) throws MailException {
		SimpleMailMessage message = new SimpleMailMessage();
		
		message.setFrom(from);
		message.setTo(to);
		message.setSubject(subject);
		message.setText(msg);
		
		mailSender.send(message);	
	}
	
	/* ------------------------------------------------------------------------------------------ */
	
	/**
	 * Sends a new email using the information available in the class instance.
	 * 
	 * @throws MailException	if the email cannot be sent
	 */
	public void sendMail() throws MailException {
		SimpleMailMessage message = new SimpleMailMessage();		 
		
		message.setFrom(from);
		message.setTo(to);
		message.setSubject(subject);
		message.setText(text);
		
		mailSender.send(message);			
	}

	/* ------------------------------------------------------------------------------------------ */
	
	/**
	 * Sends a new email based on the message received through the method's parameter.
	 * 
	 * @param message			the message to send
	 * @throws MailException	if the email cannot be sent
	 */
	public void sendMail(SimpleMailMessage message) throws MailException {
		mailSender.send(message);
	}	
	
	/* 
	================================================================================================
	 Getters and Setters
	================================================================================================
	*/
	public MailSender getMailSender() {
		return mailSender;
	}
	
	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}
	
	/* ------------------------------------------------------------------------------------------ */
	
	public String getFrom() {
		return from;
	}
	
	public void setFrom(String from) {
		this.from = from;
	}
	
	/* ------------------------------------------------------------------------------------------ */
	
	public String getTo() {
		return to;
	}
	
	public void setTo(String to) {
		this.to = to;
	}
	
	/* ------------------------------------------------------------------------------------------ */
	
	public String getSubject() {
		return subject;
	}
	
	public void setSubject(String subject) {
		this.subject = subject;
	}
	
	/* ------------------------------------------------------------------------------------------ */
	
	public String getText() {
		return text;
	}
	
	public void setText(String text) {
		this.text = text;
	}
		
}